package org.hyrskytin.viivallesoundboard;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends ListActivity {
    private ArrayList<Sound> mSounds = null;
    private SoundAdapter mAdapter = null;
    static MediaPlayer mMediaPlayer = null;
    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        initControls();

        // create a list of sounds
        mSounds = new ArrayList<Sound>();

        Sound s = new Sound();
        s.setDescription(getString(R.string.pssst));
        s.setSoundResourceId(R.raw.pssst);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.hyrskynhyrskyn));
        s.setSoundResourceId(R.raw.hyrskynhyrskyn);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.sitkuvedetaahyrskytin));
        s.setSoundResourceId(R.raw.sitkuvedetaahyrskytin);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.kuuluuvaanettahyrskyn));
        s.setSoundResourceId(R.raw.kuuluuvaanettahyrskyn);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.joku300heppanen));
        s.setSoundResourceId(R.raw.joku300heppanen);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.kuustoistaventtiiline));
        s.setSoundResourceId(R.raw.kuustoistaventtiiline);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.kahenlitranohc));
        s.setSoundResourceId(R.raw.kahenlitranohc);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.kuuteenpisteneljaansekuntiin));
        s.setSoundResourceId(R.raw.kuuteenpisteneljaansekuntiin);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.aivanvakio));
        s.setSoundResourceId(R.raw.aivanvakio);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.kyllasaaollaikakova));
        s.setSoundResourceId(R.raw.kyllasaaollaikakova);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.lyyaankov8tulille));
        s.setSoundResourceId(R.raw.lyyaankov8tulille);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.jajaa));
        s.setSoundResourceId(R.raw.jajaa);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.monenkolitrankoneella));
        s.setSoundResourceId(R.raw.monenkolitrankoneella);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.nytviivalle));
        s.setSoundResourceId(R.raw.nytviivalle);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.valitettavasti));
        s.setSoundResourceId(R.raw.valitettavasti);
        mSounds.add(s);

        s = new Sound();
        s.setDescription(getString(R.string.vapari));
        s.setSoundResourceId(R.raw.vapari);
        mSounds.add(s);

        mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
        setListAdapter(mAdapter);
        getListView().setDivider(null);
        getListView().setDividerHeight(0);

    }

    private void initControls() {
        try {
            volumeSeekbar = (SeekBar) findViewById(R.id.volume_control);
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));

            volumeSeekbar
                    .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                        @Override
                        public void onStopTrackingTouch(SeekBar arg0) {
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar arg0) {
                        }

                        @Override
                        public void onProgressChanged(SeekBar arg0,
                                int progress, boolean arg2) {
                            audioManager.setStreamVolume(
                                    AudioManager.STREAM_MUSIC, progress, 0);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onListItemClick(ListView parent, View v, int position, long id) {
        initControls();
        Sound s = mSounds.get(position);
        if (mMediaPlayer != null) {
            mMediaPlayer.stop();
            mMediaPlayer.release();
        }
        mMediaPlayer = MediaPlayer.create(this, s.getSoundResourceId());
        mMediaPlayer.start();
    }
}